package com.itbuka.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.itbuka.goods.domain.GoodsProduct;
import com.itbuka.goods.domain.GoodsProductDetails;
import com.itbuka.goods.mapper.GoodsProductDetailsMapper;
import com.itbuka.goods.mapper.GoodsProductMapper;
import com.itbuka.goods.service.GoodsProductDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


import java.util.List;

/**
 * @author ld
 * @description 针对表【goods_product_details(商品详情表)】的数据库操作Service实现
 * @createDate 2024-07-30 11:53:09
 */
@Service
public class GoodsProductDetailsServiceImpl implements GoodsProductDetailsService {
    @Autowired
    private GoodsProductDetailsMapper goodsProductDetailsMapper;

    @Autowired
    private GoodsProductMapper goodsProductMapper;

    @Override
    public List<GoodsProductDetails> selectAll() {
        LambdaQueryWrapper<GoodsProductDetails> lqw = new LambdaQueryWrapper<>();
        lqw.eq(GoodsProductDetails::getIsDelete, 0);
        return goodsProductDetailsMapper.selectList(lqw);
    }


    @Override
    public List<GoodsProductDetails> selectList(GoodsProductDetails goodsProductDetails) {
        LambdaQueryWrapper<GoodsProductDetails> lqw = this.lqw(goodsProductDetails);
        return goodsProductDetailsMapper.selectList(lqw);
    }

    @Override
    public Integer insert(GoodsProductDetails goodsProductDetails) {
        LambdaQueryWrapper<GoodsProductDetails> lqw = new LambdaQueryWrapper<>();
        lqw.eq(GoodsProductDetails::getIsDelete, 0);
        lqw.eq(GoodsProductDetails::getProductId, goodsProductDetails.getProductId());
        lqw.orderByAsc(GoodsProductDetails::getPrice);
        List<GoodsProductDetails> list = goodsProductDetailsMapper.selectList(lqw);
        if (list.size() > 0) {
            GoodsProduct goodsProduct = new GoodsProduct();
            goodsProduct.setId(goodsProductDetails.getProductId());
            if (goodsProductDetails.getPrice().compareTo(list.get(0).getPrice()) > 0) {
                goodsProduct.setPrice(list.get(0).getPrice());
            } else {
                goodsProduct.setPrice(goodsProductDetails.getPrice());
            }
            goodsProductMapper.updateById(goodsProduct);
        }
        return goodsProductDetailsMapper.insert(goodsProductDetails);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                GoodsProductDetails goodsProductDetails = new GoodsProductDetails();
                goodsProductDetails.setId(Long.parseLong(id));
                goodsProductDetails.setIsDelete(1);
                goodsProductDetailsMapper.updateById(goodsProductDetails);
            }
            return 1;
        } catch (Exception e) {
            return -1;
        }
    }

    @Override
    public Integer update(GoodsProductDetails goodsProductDetails) {
        return goodsProductDetailsMapper.updateById(goodsProductDetails);
    }

    @Override
    public Page<GoodsProductDetails> pageAll(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return (Page<GoodsProductDetails>) this.selectAll();
    }

    @Override
    public Page<GoodsProductDetails> pageList(GoodsProductDetails goodsProductDetails, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return (Page<GoodsProductDetails>) this.selectList(goodsProductDetails);
    }

    @Override
    public Integer reduceStock(Long productId, Integer num) {
        return goodsProductDetailsMapper.reduceStock(productId, num);
    }

    public LambdaQueryWrapper<GoodsProductDetails> lqw(GoodsProductDetails goodsProductDetails) {
        LambdaQueryWrapper<GoodsProductDetails> lqw = new LambdaQueryWrapper<>();
        if (goodsProductDetails.getId() != null) {
            lqw.eq(GoodsProductDetails::getId, goodsProductDetails.getId());
        }
        if (goodsProductDetails.getSpecId() != null) {
            lqw.eq(GoodsProductDetails::getSpecId, goodsProductDetails.getSpecId());
        }
        if (goodsProductDetails.getNum() != null) {
            lqw.eq(GoodsProductDetails::getNum, goodsProductDetails.getNum());
        }
        if (goodsProductDetails.getWeight() != null) {
            lqw.eq(GoodsProductDetails::getWeight, goodsProductDetails.getWeight());
        }
        if (goodsProductDetails.getPrice() != null) {
            lqw.eq(GoodsProductDetails::getPrice, goodsProductDetails.getPrice());
        }
        if (goodsProductDetails.getInventory() != null) {
            lqw.eq(GoodsProductDetails::getInventory, goodsProductDetails.getInventory());
        }
        if (goodsProductDetails.getImg() != null) {
            lqw.eq(GoodsProductDetails::getImg, goodsProductDetails.getImg());
        }
        if (goodsProductDetails.getProductId() != null) {
            lqw.eq(GoodsProductDetails::getProductId, goodsProductDetails.getProductId());
        }
        if (goodsProductDetails.getIsDelete() != null) {
            lqw.eq(GoodsProductDetails::getIsDelete, goodsProductDetails.getIsDelete());
        }
        if (goodsProductDetails.getCreateTime() != null) {
            lqw.eq(GoodsProductDetails::getCreateTime, goodsProductDetails.getCreateTime());
        }
        if (goodsProductDetails.getUpdateTime() != null) {
            lqw.eq(GoodsProductDetails::getUpdateTime, goodsProductDetails.getUpdateTime());
        }
        return lqw;
    }
}




